﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Data;
using System.Data.OracleClient;
using System.Data.Common;
using MSL.DataBase.Lib;
using System.Collections;

namespace MSL.DataBase.Core
{
    internal class OracleHelper : DbBase
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="conn">链接字符串</param>
        public OracleHelper(string conn)
            : base(DataBaseType.Oracle, conn, DBConfig.OracleClient)
        {

        }
        public OracleHelper()
            : base(DataBaseType.Oracle, DBConfig.ConnString, DBConfig.OracleClient)
        {

        }
        public override void AddReturnPara()//待验证
        {
            //if (!Com.Parameters.Contains("ResultCursor"))
            //{
            //    AddCustomePara("ResultCursor", ParaType.Cursor);
            //}
            //if (!Com.Parameters.Contains("ResultCount"))
            //{
            //    AddParameters("ResultCount", null, DbType.Int32, -1, ParameterDirection.Output);//记录总数在最后一位
            //}
        }
        internal override void AddCustomePara(string paraName, ParaType paraType)
        {
            OracleParameter para = new OracleParameter();
            para.ParameterName = paraName;
            switch (paraType)
            {
                case ParaType.Cursor:
                case ParaType.OutPut:
                    if (paraType == ParaType.Cursor)
                    {
                        para.OracleType = OracleType.Cursor;
                    }
                    else
                    {
                        para.OracleType = OracleType.Int32;
                    }
                    para.Direction = ParameterDirection.Output;
                    break;
                case ParaType.result:
                    para.OracleType = OracleType.Int32;
                    para.Direction = ParameterDirection.ReturnValue;
                    break;

            }
            Com.Parameters.Add(para);
        }
        public override System.Data.Common.DbParameter GetNewParameter()
        {
            return new OracleParameter();
        }
        public override string Pre
        {
            get
            {
                return ":";
            }
        }

        #region 分页查询

        public override DataTable Query(string tableName, string primaryKey, string showFields, int pageIndex, int pageSize, string where, string sortBy, string groupBy, out int rowCount)
        {
            rowCount = 0;
            DataTable dt = null;
            Hashtable parameters = new Hashtable();

            rowCount = ExecScalar<int>(String.Format("SELECT COUNT(*) FROM {0} WHERE {1}", tableName, where), false);
            rowCount = base.recoedTotal;
            parameters.Add("@Tables", tableName);
            parameters.Add("@PageIndex", pageIndex);
            parameters.Add("@PageSize", pageSize);
            parameters.Add("@Fields", showFields);
            parameters.Add("@PrimaryKey", primaryKey);//排序使用
            parameters.Add("@Group", null);//分组
            parameters.Add("@Sort", sortBy);
            parameters.Add("@Filter", where);

            dt = Query("sp_Paging", true, parameters);
            return dt;
        }

        #endregion

        #region 批量操作

        /// <summary>
        /// 大批量数据插入
        /// </summary>
        /// <param name="table">数据表</param>
        public override void BulkInsert(DataTable table)
        {
            return;
        }

        #endregion 批量操作

        #region 数据库管理

        /// <summary>
        /// 还原数据库
        /// </summary>
        /// <param name="dbName">数据库名</param>
        /// <param name="backfile">备份文件</param>
        /// <returns>还原成功与否提示</returns>
        public override bool RestoreDatabase(string dbName, string backfile)
        {
            return false;
        }

        /// <summary>
        /// 备份数据库
        /// </summary>
        /// <param name="dbName">数据库名</param>
        /// <param name="dbFileName">备份文件</param>
        /// <returns>是否备份成功</returns>
        public override bool BackDataBase(string dbName, string dbFileName)
        {
            return false;
        }

        /// <summary>
        /// 新建数据库
        /// </summary>
        /// <param name="databaseName">数据库名</param>
        public override bool CreateDataBase(string databaseName)
        {
            return false;
        }

        /// <summary>
        /// 删除数据库
        /// </summary>
        public override bool DropDataBase(string databaseName)
        {
            return false;
        }
        #endregion
    }
}